<template>
  <!-- 百人场(飞机)详情 -->
  <el-dialog v-if="props.show" :modelValue="props.show" custom-class="detailData" width="60%" :title="GameName(indexStore.gameArray, props.checkedItem.game_id)" :close-on-click-modal="false"
    :close-on-press-escape="false" @close="onHideDetails">
    <div class="border-box">
      <template v-for="(item, index) in data.filArr" :key="index">
        <div class="flex list">
          <div class="width20">
            <span v-if="item.key!='game_board'">{{ item.label }}</span>
            <span v-else>
              {{ item.label }}
              <span class="colorRed marginL2" v-show="data.listDetails.length">({{ (data.gameIndex+1)+'/'+data.listDetails.length }})</span>
            </span>
          </div>
          <div style="position: relative">
            <span v-if="item.key=='game_id'">
              <div class="gameIcon marginB5">
                <div class="flex marginR5">
                  <el-tag size="small">{{ GamePlatform(indexStore.gameArray, props.checkedItem.game_id) }}</el-tag>
                  <img class="game-icon" :src="selectToLabel(GAMEICON, props.checkedItem.game_id)" alt="">
                </div>
                {{ selectToLabel(indexStore.gameArray, props.checkedItem.game_id) }}
              </div>
            </span>
            <span v-else-if="item.key=='win_lose'||item.key=='bet'||item.key == 'win'||item.key=='enter_money'||item.key == 'after_settlement_money' " 
            :class="colorClass(item.value)">{{ BigWan(item.value) }}</span>
            <div v-else-if="item.key=='game_board' && item.value">
              <iframe class="marginT5" :src="item.value" width="480px" height="750px" allow-popups style="border: 1px solid #ededed;"/>
              <div v-show="(data.listDetails.length && data.gameIndex!= 0)?true:false" class="slide leftSlide" @click="checkLeft">
                <img :src="Left" alt="">
              </div>
              <div v-show="(data.listDetails.length && data.gameIndex!=data.listDetails.length-1)?true:false" class="slide rightSlide" @click="checkRight">
                <img :src="Right" alt="">
              </div>
            </div>
            <span v-else>{{ item.value }}</span>
          </div>
        </div>
      </template>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, watch } from 'vue'
import { setTimestampTime } from '@/utils/time'
import { BigWan, BigMinus } from '@/utils/math'
import { GAMEICON } from '@/config/eunm'
import { selectToLabel, colorClass, sortObjectArray,GameName,GamePlatform } from '@/utils/index'
import { getGameDetails,getGameMiniReport } from '@/api/user'

import { useStore } from '@/stores/index'
const indexStore = useStore()
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
import Left from "@/assets/images/icon/left.png"
import Right from "@/assets/images/icon/right.png"

const props = defineProps({
  checkedItem: Object as any, // 当前条数的数据 
  show: Boolean, // 是否显示
})

const data = reactive({
  filArr: [] as any[],
  listDetails: [] as any[],
  gameIndex: 0
})

watch(
  () => [props.checkedItem,props.show],
  async (value) => {
    if (value[0] && value[1]) {
      gameDetails(props.checkedItem)
      gameRecordDetails()
    }
  },
)

const gameDetails = async(row:any) => {
  let dataRes = await getGameDetails({
    uuid:row.uuid_str,
    uid:row.uid,
    agent_id:row.agent_id,
  })
  let res = JSON.parse(dataRes.data.log_json)
  console.log('老虎机详情'+'('+dataRes.data.round_id_str+')',res);
  data.filArr = [
    { label: t('36'), value: GameName(indexStore.gameArray, props.checkedItem.game_id), key: 'game_id' },
    { label: t('40'), value: dataRes.data.round_id_str, key: 'round_id_str' },
    { label: t('39'), value: setTimestampTime(dataRes.data.save_time_stamp), key: 'created_time_stamp' },
    { label: t('394'), value: dataRes.data.game_board, key: 'game_board' },
    { label: t('42'), value: dataRes.data.enter_money, key: 'enter_money' },
    { label: t('43'), value: dataRes.data.after_settlement_money, key: 'after_settlement_money' },
    { label: t('44'), value: dataRes.data.bet, key: 'bet' },
    { label: t('45'), value: BigMinus(dataRes.data.win,dataRes.data.bet), key: 'win_lose' },
    { label: t('58'), value: dataRes.data.win, key: 'win' },
    // { label: t('402'), value: res.lw?res.lw:res.sw, key: 'lw' },
  ]
}

// 游戏特殊玩法(连消+小游戏)
const gameRecordDetails = async() => {
  data.listDetails = []
  data.gameIndex = 0
  if(props.checkedItem.small_game_type){
    let res = await getGameMiniReport({
      round_id:props.checkedItem.round_id_str?props.checkedItem.round_id_str:props.checkedItem.parent_id_str,
      uid:props.checkedItem.uid,
      agent_id:props.checkedItem.agent_id,
    })
    // data.listDetails = sortObjectArray(res.data.list, 'id', 'asc')
    data.listDetails = res.data.list
    data.listDetails.forEach((item:any,index:number)=> {
      if(props.checkedItem.round_id_str == item.round_id_str){
        data.gameIndex = index
      }
    })
  }
}

// 查看牌局游戏详情
const checkLeft = () => {
  if(!data.gameIndex) return
  data.gameIndex = data.gameIndex - 1
  gameDetails(data.listDetails[data.gameIndex])
}
const checkRight = () => {
  if(data.gameIndex == data.listDetails.length-1) return
  data.gameIndex = data.gameIndex + 1
  gameDetails(data.listDetails[data.gameIndex])
}

// 重置
const emit = defineEmits(['onHideDetails'])
const onHideDetails = () => {
  emit('onHideDetails', false)
}
</script>
<style scoped lang="scss">
.border-box {
  padding-bottom: 40px;
  font-size: 14px;
  color: var(--text);
}

.list {
  font-weight: bold;
  min-height: 36px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--el-border-color-lighter);
}
.slide{
  width: 50px;
  height: 50px;
  border-radius: 50%;
  cursor: pointer;
  text-align: center;
}
.rightSlide{
  position: absolute;
  right: -12%;
  top: 42%;
}
.leftSlide{
  position: absolute;
  left: -12%;
  top: 42%;
}
</style>